package ac.neec.mio.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import ac.neec.data.HeartRate;
import ac.neec.listener.EventsCallBackListener;
import ac.neec.listener.HeartRateEventListener;
import ac.neec.mio.R;
import ac.neec.mio.profile.ProfilePreferenceSetting;
import ac.neec.mio.resting.RestingMeasurementManager;
import ac.neec.mio.ui.activity.RestingMeasurementActivity;
import ac.neec.timewatch.TimeWatchInterface;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class RestingMeasurementFragment extends Fragment implements TimeWatchInterface, HeartRateEventListener<String> {

	private boolean measurementFlag = false;
	private HeartRate heartRate;
	private Context context;
	private Button startButton;
	private RestingMeasurementManager restingMeasurementManager;
	private EventsCallBackListener mCallbackListener;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mCallbackListener = (EventsCallBackListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnArticleSelectedListener");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedStateInstance) {

		context = inflater.getContext();
		View view = inflater.inflate(R.layout.activity_resting,
				container, false);

		linkView(view);
		linkListener();

		restingMeasurementManager = new RestingMeasurementManager(this);

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		return view;
	}

	private void linkView(View view) {
		startButton = (Button) view.findViewById(R.id.fragment_resting_measurement_btn_start);
	}

	private void linkListener() {
		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				heartRate = new HeartRate();
				mCallbackListener.onCallBack(RestingMeasurementActivity.CALLBACK_ID_MEASUREMENT_START, null);
				measurementFlag = true;
				restingMeasurementManager.showRestingDialog(getFragmentManager());
			}
		});
	}

	@Override
	public void notifyTimerUpdata(long time) {
	}

	@Override
	public void onCancel() {
		measurementFlag = false;
		heartRate = null;
	}

	private void setResting(HeartRate heartRate2) {

		List<Integer> list = new ArrayList<Integer>();
		list.addAll(heartRate.getHeartRates());
		int heart = 0;
		for (int t : list) {
			heart += t;
		}
		heart /= restingMeasurementManager.RESTING_MEASUREMENT_TIME / 100;
		Log.e("", "restingHeart = " + heart);
	}

	@Override
	public void onAlarm() {
		measurementFlag = false;
		//		setResting(heartRate);
		mCallbackListener.onCallBack(
				RestingMeasurementActivity.CALLBACK_ID_MEASUREMENT_STOP,
				heartRate);
		//		heartRate = null;
	}

	@Override
	public void notifyHeartRateEvent(String value) {
		if (measurementFlag) {
			Log.e("", "value = " + value);
			heartRate.addHeartRate(Integer.valueOf(value));
			ProfilePreferenceSetting.setRestingHeart(heartRate.getRestingHeartRate());
		}
	}

}
